package comlib

import chisel3._
import chisel3.util._

class MaskerIO( size:Int) extends Bundle{
  
  val in = Input(Vec(size,Bool()))
  val update = Output(Bool())

  val msk = Input(Bool())
  val msk_bmp = Input(Vec(size, Bool()))
  
  val valids = Output(Vec(size, Bool()))

  override def cloneType = new MaskerIO(size).asInstanceOf[this.type]
}

class Masker(size:Int) extends Module{
  val io = IO(new MaskerIO(size))
  val mask = RegInit(VecInit(Seq.fill(size)(false.B)))
  val isOnehot = (io.valids.asUInt & (io.valids.asUInt-1.U)) === 0.U

  when(io.msk){
    when(isOnehot){
      (0 until size).foreach {case i => mask(i) := false.B}
    }.otherwise{
      (0 until size).foreach {case i => mask(i) := mask(i) | io.msk_bmp(i)}
    }
  }

  io.valids := VecInit( (0 until size).map {case i=> io.in(i)&(!mask(i))})
  io.update := io.msk & isOnehot
}

